package com.xk857.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.exception.DefaultException;
import com.xk857.modules.entities.Student;
import com.xk857.modules.entities.req.FindStudentReq;
import com.xk857.modules.entities.req.StudentReq;
import com.xk857.modules.entities.vo.StudentSimpleVo;
import com.xk857.modules.mapper.StudentMapper;
import com.xk857.modules.service.IStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cv大魔王
 * @version 1.0
 * @date 2021/9/5 10:51
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    private final String TOKEN = "z305305.";

    @Override
    public Student add(StudentReq req) {
        Student student = new Student();
        student.setRealName(req.getRealName());
        student.setBirthday(req.getBirthday());
        student.setGender(req.getGender());
        student.setClasses(req.getClasses());
        student.setPhone(req.getPhone());
        student.setQq(req.getQq());
        student.setEducation(req.getEducation());
        student.setOldSchool(req.getOldSchool());
        student.setHometown(req.getHometown());
        student.setPlan(req.getPlan());
        student.setProgramming(req.getProgramming());
        student.setRemarks(req.getRemarks());
        student.setCreateTime(new Date());
        baseMapper.insert(student);
        return student;
    }

    @Override
    public List<StudentSimpleVo> listStudentSimple() {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.select("real_name", "classes", "create_time");
        List<Student> students = baseMapper.selectList(wrapper);

        List<StudentSimpleVo> voList = new ArrayList<>();
        students.forEach(student -> {
            StudentSimpleVo vo = new StudentSimpleVo();
            vo.setRealName(student.getRealName());
            vo.setClasses(student.getClasses());
            vo.setCreateTime(student.getCreateTime());
            voList.add(vo);
        });

        return voList;
    }

    @Override
    public List<Student> listAll(FindStudentReq req) {
        if (!TOKEN.equals(req.getToken())) {
            throw new DefaultException("权限不足");
        }
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(req.getRealName())) {
            wrapper.like("real_name", req.getRealName());
        }
        if (req.getGender() != null) {
            if (req.getGender()) {
                wrapper.eq("gender", 1);
            } else {
                wrapper.eq("gender", 0);
            }
        }
        if (StringUtils.isNotBlank(req.getClasses())) {
            wrapper.eq("classes", req.getClasses());
        }
        if (req.getEducation() != null) {
            wrapper.eq("education", req.getEducation());
        }

        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<String> findAllClasses() {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT classes");
        List<Student> students = baseMapper.selectList(wrapper);
        List<String> classes = new ArrayList<>();
        students.forEach(student -> classes.add(student.getClasses()));
        return classes;
    }


}
